package com.holi.hiteacher.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Joshua Wong on 2016/5/6.
 */
public class DataUtils {
    /**
     * 日期格式转字符串格式
     *
     * @param date
     * @return
     */
    public static String date2str_type1(Date date) {
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMddHHmm", Locale.CHINA);
        return sfDate.format(date);
    }

    public static String date2str_type2(Date date) {
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
        return sfDate.format(date);
    }

    public static String date2str_type3(Date date) {
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        return sfDate.format(date);
    }

    public static Date str2dateType1(String str) {
        Date date = null;
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMddHHmm", Locale.CHINA);
        try {
            date = sfDate.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date str2dateType2(String str) {
        Date date = null;
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            date = sfDate.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String dateFormat(String time) {//201605211000
        String result;
        result = time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8)
                + " " + time.substring(8, 10) + ":" + time.substring(10, 12);
        return result;
    }

    public static Date dateDayAdd(Date date, int num) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.DAY_OF_MONTH, num);
        gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
        return gc.getTime();
    }

    public static Date dateMonthAdd(Date date, int num) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(GregorianCalendar.MONTH, num);
        gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
        return gc.getTime();
    }

    /**
     * 检查字符串是否为空
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        if (s == null)
            return true;
        else if (s.length() == 0)
            return true;
        else if (s.trim().length() == 0)
            return true;
        return false;
    }

    // 校验Tag Alias 只能是数字，英文字母和中文
    public static boolean isValidTagAndAlias(String s) {
        Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public static boolean isConnected(Context context) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context
                .CONNECTIVITY_SERVICE);
        NetworkInfo info = conn.getActiveNetworkInfo();
        return (info != null && info.isConnected());
    }

}
